package com.appbee.core.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.ButtonBase;
import com.google.gwt.user.client.ui.impl.FocusImpl;

public class ToggleButton extends ButtonBase
{
	private boolean value;
	private String onClass;
	private String offClass;

	public ToggleButton(String onClass, String offClass)
	{
		super(FocusImpl.getFocusImplForPanel().createFocusable());
		this.onClass = onClass;
		this.offClass = offClass;
		setValue(false);
		addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				setValue(!value);
			}
		});
		addKeyDownHandler(new KeyDownHandler()
		{

			@Override
			public void onKeyDown(KeyDownEvent event)
			{
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
				{
					setValue(!value);
				}
			}
		});
	}

	public boolean getValue()
	{
		return value;
	}

	public void setValue(boolean value)
	{
		this.value = value;
		setStyleName(value ? onClass : offClass);

	}

	public String getOnClass()
	{
		return onClass;
	}

	public void setOnClass(String onClass)
	{
		this.onClass = onClass;
	}

	public String getOffClass()
	{
		return offClass;
	}

	public void setOffClass(String offClass)
	{
		this.offClass = offClass;
	}

}
